package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.Logquotaview;
import com.example.recordingpen_datacollection.entity.Query.LogquotaviewQue;
import com.example.recordingpen_datacollection.mapper.LogquotaviewMapper;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.LogquotaviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2024-06-13
 */
@Service
public class LogquotaviewServiceImpl extends ServiceImpl<LogquotaviewMapper, Logquotaview> implements LogquotaviewService {

    @Override
    public RLIST getLogquotaPage(LogquotaviewQue logquotaviewQue, Integer value) {
        QueryWrapper wrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(logquotaviewQue.getUName())){
            wrapper.like("uname",logquotaviewQue.getUName());
        }
        if(StringUtils.isNotEmpty(logquotaviewQue.getDevName())){
            wrapper.like("devname",logquotaviewQue.getDevName());
        }
        if(value !=null && value>0){
            wrapper.eq("uid",value);
        }

        wrapper.orderByDesc("log_quotaid");
        Page<Logquotaview> page = new Page<>(logquotaviewQue.getPageNo(), logquotaviewQue.getPageSize());
        this.page(page, wrapper);
        long total = page.getTotal();
        List<Logquotaview> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)logquotaviewQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);
    }
}
